package com.cmict.data.system.service.encode;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.framework.mybatis.core.query.QueryWrapperX;
import com.cmict.data.system.api.user.AdminUserApi;
import com.cmict.data.system.controller.admin.encode.vo.EncodeCreateReqVO;
import com.cmict.data.system.controller.admin.encode.vo.EncodeDataPageReqVO;
import com.cmict.data.system.dal.dataobject.encode.EncodeDO;
import com.cmict.data.system.dal.dataobject.encode.EncodeDetailsDO;
import com.cmict.data.system.dal.mysql.encode.EncodeDetailsMapper;
import com.cmict.data.system.dal.mysql.encode.EncodeMapper;
import com.cmict.data.system.enums.encode.EnCodeDetailTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import static com.cmict.data.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.cmict.data.system.enums.ErrorCodeConstants.ENCODE_NAME_EXIST_ERROR;
import static com.cmict.data.system.enums.ErrorCodeConstants.ENCODE_NUMBER_EXIST_ERROR;

@Service
@Slf4j
public class EncodeServiceImpl implements EncodeService {

    @Resource
    private EncodeMapper encodeMapper;

    @Resource
    private EncodeDetailsMapper encodeDetailsMapper;

    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public Long createEncodeData(EncodeCreateReqVO reqVO) {
        if(encodeMapper.selectCount(EncodeDO::getEncodeName,reqVO.getEncodeName()) > 0){
            throw exception(ENCODE_NAME_EXIST_ERROR);
        }
        if(encodeMapper.selectCount(EncodeDO::getEncodeNum,reqVO.getEncodeNum()) > 0){
            throw exception(ENCODE_NUMBER_EXIST_ERROR);
        }

        encodeMapper.insert(reqVO);
        return reqVO.getId();
    }

    @Override
    public void updateEncodeData(EncodeCreateReqVO reqVO) {

        Long aLong = encodeMapper.selectCount(new QueryWrapper<EncodeDO>().lambda()
                .eq(EncodeDO::getEncodeName, reqVO.getEncodeName())
                .ne(EncodeDO::getId, reqVO.getId())
        );
        if(aLong > 0){
            throw exception(ENCODE_NAME_EXIST_ERROR);
        }
        Long bLong = encodeMapper.selectCount(new QueryWrapper<EncodeDO>().lambda()
                .eq(EncodeDO::getEncodeName, reqVO.getEncodeNum())
                .ne(EncodeDO::getId, reqVO.getId())
        );
        if(bLong > 0){
            throw exception(ENCODE_NUMBER_EXIST_ERROR);
        }
        encodeMapper.updateById(reqVO);
    }

    @Override
    public void deleteEncodeData(Long id) {
        encodeMapper.deleteById(id);
    }

    @Override
    public void deleteEncodeDataBatch(List<Long> ids) {
        String encodeIds ="'"+ StringUtils.join(ids,"','")+"'";
        Set<Long> set =  encodeDetailsMapper.getEncodeDetailsId(encodeIds);
        if(set.size() > 0) {
            encodeDetailsMapper.deleteBatchIds(set);
        }
        encodeMapper.deleteBatchIds(ids);
    }

    @Override
    public PageResult<EncodeDO> getDictEncodePage(EncodeDataPageReqVO reqVO) {
        PageResult<EncodeDO> encodeDOPageResult = encodeMapper.selectPage(reqVO);
        List<EncodeDO> list = encodeDOPageResult.getList();
        for (EncodeDO encodeDO : list) {
            List<String> applyFieldList = new ArrayList<>();
            List<EncodeDetailsDO> encodeDetailsDOS = encodeDetailsMapper.selectList(new QueryWrapperX<EncodeDetailsDO>().lambda()
                    .eq(EncodeDetailsDO::getPrefixion, EnCodeDetailTypeEnum.DATA_ASSOCIATION.getPrefixion())
                    .eq(EncodeDetailsDO::getEncodeId,encodeDO.getId())
            );
//            for (EncodeDetailsDO encodeDetailsDO : encodeDetailsDOS) {
//                if(StringUtils.isNoneBlank(encodeDetailsDO.getApplyField())){
//                    applyFieldList.add(encodeDetailsDO.getApplyField());
//                }
//            }
            encodeDO.setApplyFieldList(encodeDetailsDOS);
            encodeDO.setCreateName(adminUserApi.getUser(Long.valueOf(encodeDO.getCreator())).getCheckedData().getUsername());
        }
        return  encodeDOPageResult;
    }

    @Override
    public EncodeDO getEncodeData(Long id) {
        return encodeMapper.selectById(id);
    }
}
